package com.anyi.gulimall.coupon.service.impl;

import com.anyi.gulimall.common.to.SkuReductionTo;
import com.anyi.gulimall.coupon.controller.SmsSkuFullReductionController;
import com.anyi.gulimall.coupon.entity.SmsMemberPriceEntity;
import com.anyi.gulimall.coupon.entity.SmsSkuLadderEntity;
import com.anyi.gulimall.coupon.service.SmsMemberPriceService;
import com.anyi.gulimall.coupon.service.SmsSkuFullReductionService;
import com.anyi.gulimall.coupon.service.SmsSkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.anyi.gulimall.common.utils.PageUtils;
import com.anyi.gulimall.common.utils.Query;

import com.anyi.gulimall.coupon.dao.SmsSkuFullReductionDao;
import com.anyi.gulimall.coupon.entity.SmsSkuFullReductionEntity;


@Service("smsSkuFullReductionService")
public class SmsSkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionDao, SmsSkuFullReductionEntity> implements SmsSkuFullReductionService {

    @Autowired
    private SmsSkuLadderService smsSkuLadderService;


    @Autowired
    private SmsMemberPriceService smsMemberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSkuFullReductionEntity> page = this.page(
                new Query<SmsSkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SmsSkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存折扣信息和会员价格信息
     * @param skuReductionTo
     */
    @Override
    public void saveReductionAndMemberPrice(SkuReductionTo skuReductionTo) {
        // 保存折扣信息
        SmsSkuFullReductionEntity smsSkuFullReductionEntity = new SmsSkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,smsSkuFullReductionEntity);


        this.save(smsSkuFullReductionEntity);
        // 保存满减打折信息
        SmsSkuLadderEntity smsSkuLadderEntity = new SmsSkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo,smsSkuLadderEntity);
        smsSkuLadderEntity.setPrice(skuReductionTo.getFullPrice().subtract(skuReductionTo.getReducePrice()));
        smsSkuLadderService.save(smsSkuLadderEntity);
        // 保存会员价格信息
        List<SmsMemberPriceEntity> collect = skuReductionTo.getMemberPrice().stream().map(item -> {
            SmsMemberPriceEntity smsMemberPriceEntity = new SmsMemberPriceEntity();
            smsMemberPriceEntity.setMemberLevelId(item.getId());
            smsMemberPriceEntity.setMemberLevelName(item.getName());
            smsMemberPriceEntity.setMemberPrice(item.getPrice());
            return smsMemberPriceEntity;
        }).collect(Collectors.toList());
        smsMemberPriceService.saveBatch(collect);
    }

}